package com.mssh.login.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mssh.entity.table.SysUser;
import com.mssh.login.mapper.SysUserMapper;
import com.mssh.login.service.UserService;
import com.mssh.login.util.CommonUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 熳殊沙华
 * @Date: 2022/09/10/16:39
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {
    private Logger logger = LogManager.getLogger(this.getClass());

    @Autowired
    private SysUserMapper userMapper;

//    @Autowired
//    private SysParamsCacheHanlder sysParamsCacheHanlder;


    /**
     * 根据账号查询用户id
     * @param account
     * @return
     */
    @Override
    public JSONObject getUserByAccount(String account) throws Exception{
        JSONObject result = new JSONObject();
        if(account == null || account.isEmpty()){
            result.put("errCode","9999");
            result.put("errMsg","查询条件为空！");
            return result;
        }
        //根据登录账号查询user对象（包含了该用户对应的所有的资源id）
        SysUser user = null;
        //根据账号长度判断是否为手机号登录
        if(account.length() == 11 && CommonUtil.verifyPhone(account)){
            user = userMapper.getSysUserByTelphone(account);
        }else{
            //构建认证对象
            user = userMapper.getSysUserByAccount(account);
        }
        if(user == null){
            result.put("errCode","9999");
            result.put("errMsg","未查询到当前用户信息！");
            return result;
        }
        logger.info("当前查询到的用户信息为：{}", JSONObject.toJSONString(user));
        //获取头像ip端口
//        SysParams headerImgParams = sysParamsCacheHanlder.getCacheByKey("headImgIpPort");
//        if(headerImgParams == null || CommonUtil.isEmpty(headerImgParams.getParamsValue1()) ){
//            result.put("errCode","E005");
//            result.put("errMsg","获取头像图片ip端口配置参数失败！");
//            logger.warn("获取头像图片ip端口配置参数失败！");
//            return result;
//        }
//        StringBuffer headImgIpPort = new StringBuffer();
//        headImgIpPort.append(headerImgParams.getParamsValue1()).append(user.getHeaderImg());
        JSONObject custInfo = new JSONObject();
        custInfo.put("userId", user.getId());
        custInfo.put("userName", user.getUserName());
        custInfo.put("userAccount", user.getAccount());
        custInfo.put("realName", user.getRealName());
        custInfo.put("sex", user.getSex());
        custInfo.put("age", user.getAge());
        custInfo.put("telphone", user.getTelphone());
        custInfo.put("email", user.getEmail());
        custInfo.put("address", user.getAddress());
        custInfo.put("birthday", user.getBirthday());
//        custInfo.put("headerImg", headImgIpPort.toString());
        result.put("errCode", "0000");
        result.put("errMsg", "SUCCESS");
        result.put("user", custInfo);
        return result;
    }

}
